package main;

import java.io.IOException;

import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import control.Button;
import control.ControlCommand;

public class MenuCanvas extends GameCanvas {
	protected Button[] listButton;
	protected final MainMidlet game;
	protected Image background;

	protected MenuCanvas(MainMidlet midlet) {
		super(true);
		this.game = midlet;
		setFullScreenMode(true);
		try {
			background = Image.createImage("/background.png");
		} catch (IOException e) {
			e.printStackTrace();
		}

		int sW = getWidth();
		int bW = 155;
		listButton = new Button[5];

		listButton[0] = new Button(Res.IMG_BUTTON, bW, 40, (sW - bW) / 2,
				190, "PLAY");
		listButton[0].setCommand(new ControlCommand() {
			public void Do() {
				game.showSellectGameCanvas();
			}
		});

		listButton[1] = new Button(Res.IMG_BUTTON, bW, 40, (sW - bW) / 2,
				230, "OPTION");
		listButton[1].setCommand(new ControlCommand() {
			public void Do() {
				game.showOptionCanvas();
			}
		});
		
		listButton[2] = new Button(Res.IMG_BUTTON, bW, 40, (sW - bW) / 2,
				270, "HELP");
		listButton[2].setCommand(new ControlCommand() {
			public void Do() {
				game.showHelpCanvas();
			}
		});
		
		listButton[3] = new Button(Res.IMG_BUTTON, bW, 40, (sW - bW) / 2,
				310, "ABOUT");
		listButton[3].setCommand(new ControlCommand() {
			public void Do() {
				game.showAboutCanvas();
			}
		});

		listButton[4] = new Button(Res.IMG_BUTTON, bW, 40, (sW - bW) / 2,
				350, "EXIT");
		listButton[4].setCommand(new ControlCommand() {
			public void Do() {
				game.destroyApp(false);
				game.notifyDestroyed();
			}
		});
	}

	protected void pointerPressed(int x, int y) {
		for (int i = 0; i < listButton.length; i++) {
			listButton[i].pointerPressed(x, y);
		}
		repaint();
	}

	protected void pointerReleased(int x, int y) {
		for (int i = 0; i < listButton.length; i++) {
			listButton[i].pointerReleased(x, y);
		}
		repaint();
	}

	protected void pointerDragged(int x, int y) {
		for (int i = 0; i < listButton.length; i++) {
			listButton[i].pointerPressed(x, y);
		}
		repaint();
	}

	public void paint(Graphics g) {
		g.drawImage(background, 0, 0, 0);
		for (int i = 0; i < listButton.length; i++) {
			listButton[i].paint(g);
		}
	}
}
